<template>
  <!--  供应商对账-->
  <div class="salesMargin">
    <jc-button
      :btn-arr="['reload', 'option']"
      :disab-arr="['close', 'cancel', 'using', 'freeze', 'com1', 'com2', 'chart']"
      @refresh="getData"
      @toExcel="handleExport"
    />
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuery"
        />
        <el-button
          type="primary"
          class="btn"
          size="mini"
          @click="handleQuery"
        >
          {{ $t('btn.search') }}
        </el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuery"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="getData"
      />
    </div>
  </div>
</template>

<script>
import { InStockInfo_info } from '@/api/financial/reconciliation'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  name: 'SalesMargin',
  components: {
    search,
    filtration,
    jcButton
  },
  mixins: [searData],
  data() {
    return {
      ftype: 21,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.billType'), prop: 'fbilltypename' },
        { label: this.$t('market.suppliername'), prop: 'fsuppliername', minWidth: '130px' },
        { label: this.$t('market.fdate'), prop: 'fdate', width: '80px' },
        { label: this.$t('market.deliveryno'), prop: 'fdeliveryno' },
        { label: this.$t('market.rkdh'), prop: 'finstockbillno' },
        { label: this.$t('market.purchaseno'), prop: 'fpurbillno' },
        { label: this.$t('market.fmaterial'), prop: 'fmaterialnum', width: '100' },
        { label: this.$t('market.paezText1'), prop: 'fpaeznum', minWidth: '110px' },
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('market.productName'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification' },
        { label: this.$t('market.funit'), prop: 'fmatunit', width: '80' },
        { label: this.$t('market.purchaserQty'), prop: 'fpurqty', align: 'right', width: '80' },
        { label: this.$t('market.plannum'), prop: 'plannum', align: 'right', width: '80' },
        { label: this.$t('market.purinstockqty'), prop: 'fpurinstockqty', align: 'right', width: '120' },
        { label: this.$t('market.realQty'), prop: 'frealqty', align: 'right', width: '80' },
        { label: this.$t('market.ftaxPrice'), prop: 'ftaxprice', align: 'right', width: '80' },
        { label: this.$t('market.allamount'), prop: 'fallamount', align: 'right', width: '80' },
        { label: this.$t('market.settleCurr'), prop: 'fcurrencyname', width: '80' },
        { label: this.$t('market.settleType'), prop: 'fsettlename' },
        { label: this.$t('market.note'), prop: 'fnote' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.getData()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          InStockInfo_info({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '供应商对账表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取供应商对账列表数据
    async getData(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      await InStockInfo_info(DATA).then(res => {
        if (res.code === 0) {
          this.loading = false
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.loading = false
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 搜索
    handleQuery(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.getData(ev.detail)
      } else {
        this.getData()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.salesMargin {
  @include listBom;
  .header {
    position: relative;
    .header-name {
      width: 100%;
    }
    .btn {
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
